package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;

import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;

import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;

import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderService.sunmit(orders);
        return R.success("下单成功");
    }

    //订单明细
    /*
    1.根据订单号模糊查询，以及创建日期到结束日期
    2.分页查询
    3.显示的数据有订单号，订单状态，用户，手机号，地址，下单时间，实收金额，操作

    * */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Orders orders, String beginTime, String endTime) {
        //创建分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        //根据时间查询
        lqw.lt(endTime != null, Orders::getCheckoutTime, endTime).
                gt(beginTime != null, Orders::getOrderTime, beginTime);
        //订单号模糊查询
        lqw.like(orders.getNumber() != null, Orders::getNumber, orders.getNumber());
        //下单用户

        //排序
        lqw.orderByDesc(Orders::getOrderTime);
        //查询v
        Page<Orders> page1 = orderService.page(pageInfo, lqw);
        return R.success(page1);
    }

    //更改订单状态
    @PutMapping
    public R<String> updata(@RequestBody Orders orders) {
        //根据订单号修改订单状态
        LambdaUpdateWrapper<Orders> luw = new LambdaUpdateWrapper<>();
        luw.eq(orders.getId() != null, Orders::getId, orders.getId());
        luw.set(Orders::getStatus, orders.getStatus());
        orderService.update(luw);
        //修改完查询订单明细表
        return R.success("修改成功");
    }

    //移动端查询最新订单
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize) {
        //根据付款时间查询
        Long currentId = BaseContext.getCurrentId();
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, currentId);
        //根据日期排序
        lqw.orderByDesc(Orders::getCheckoutTime);

        orderService.page(pageInfo, lqw);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        BeanUtils.copyProperties(pageInfo, ordersDtoPage, "records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list=records.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(id != null, OrderDetail::getOrderId, id);
            List<OrderDetail> orderDetails = orderDetailService.list(qw);
            ordersDto.setOrderDetails(orderDetails);
            return ordersDto;
        }).collect(Collectors.toList());
        Page<OrdersDto> ordersDtoPage1 = ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage1);

    }

    //再来一单(不够完善，无法跳转到支付页面，只能连带购物车跳到首页)
    @PostMapping("/again")
    public R<List<ShoppingCart>> again(@RequestBody Orders orders) {
        //先对购物车进行清理
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        //根据id去查询对应的订单细节
        LambdaQueryWrapper<OrderDetail> lqwod = new LambdaQueryWrapper<>();
        lqwod.eq(OrderDetail::getOrderId, orders.getId());
        //根据订单ID查询到订单详细
        List<OrderDetail> list = orderDetailService.list(lqwod);
        //把集合中数据进行遍历赋值给购物车
        ArrayList<ShoppingCart> shoppingList = new ArrayList<>();
        list.forEach(item -> {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setName(item.getName());
            shoppingCart.setImage(item.getImage());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setDishId(item.getDishId());
            shoppingCart.setSetmealId(item.getSetmealId());
            shoppingCart.setDishFlavor(item.getDishFlavor());
            shoppingCart.setNumber(item.getNumber());
            shoppingCart.setAmount(item.getAmount());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingList.add(shoppingCart);
        });
        shoppingCartService.saveBatch(shoppingList);

        return R.success(shoppingList);


    }


}
